from sqlalchemy import Column, String, Integer, ForeignKey, DateTime, Enum, Text, Float
from sqlalchemy.orm import relationship
from enum import Enum as PyEnum

from app.models.base import Base


class RiskStatus(PyEnum):
    IDENTIFIED = "identified"  # 已识别
    ANALYZED = "analyzed"  # 已分析
    PLANNED = "planned"  # 已计划应对
    MONITORED = "monitored"  # 监控中
    CLOSED = "closed"  # 已关闭
    OCCURRED = "occurred"  # 已发生


class RiskProbability(PyEnum):
    VERY_LOW = "very_low"  # 很低
    LOW = "low"  # 低
    MEDIUM = "medium"  # 中
    HIGH = "high"  # 高
    VERY_HIGH = "very_high"  # 很高


class RiskImpact(PyEnum):
    NEGLIGIBLE = "negligible"  # 可忽略
    MINOR = "minor"  # 轻微
    MODERATE = "moderate"  # 中等
    MAJOR = "major"  # 严重
    CRITICAL = "critical"  # 致命


class IssueStatus(PyEnum):
    OPEN = "open"  # 打开
    IN_PROGRESS = "in_progress"  # 处理中
    RESOLVED = "resolved"  # 已解决
    CLOSED = "closed"  # 已关闭
    REOPENED = "reopened"  # 重新打开


class IssuePriority(PyEnum):
    LOW = "low"  # 低
    MEDIUM = "medium"  # 中
    HIGH = "high"  # 高
    CRITICAL = "critical"  # 紧急


class Risk(Base):
    """风险模型"""
    name = Column(String(100), nullable=False, index=True)
    description = Column(Text, nullable=True)
    status = Column(String(20), nullable=False, default=RiskStatus.IDENTIFIED.value)
    probability = Column(String(20), nullable=False, default=RiskProbability.MEDIUM.value)
    impact = Column(String(20), nullable=False, default=RiskImpact.MODERATE.value)
    risk_score = Column(Float, nullable=True)  # 风险评分（概率*影响）
    trigger_events = Column(Text, nullable=True)  # 触发事件
    response_strategy = Column(Text, nullable=True)  # 应对策略
    contingency_plan = Column(Text, nullable=True)  # 应急计划
    identified_date = Column(DateTime, nullable=False)
    updated_date = Column(DateTime, nullable=True)
    closed_date = Column(DateTime, nullable=True)
    
    # 外键
    project_id = Column(Integer, ForeignKey("project.id"), nullable=False)
    creator_id = Column(Integer, ForeignKey("user.id"), nullable=False)
    assignee_id = Column(Integer, ForeignKey("user.id"), nullable=True)
    
    # 关系
    project = relationship("Project", back_populates="risks")
    creator = relationship("User", foreign_keys=[creator_id], back_populates="created_risks")
    assignee = relationship("User", foreign_keys=[assignee_id], back_populates="assigned_risks")
    comments = relationship("RiskComment", back_populates="risk")


class RiskComment(Base):
    """风险评论模型"""
    content = Column(Text, nullable=False)
    
    # 外键
    risk_id = Column(Integer, ForeignKey("risk.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("user.id"), nullable=False)
    
    # 关系
    risk = relationship("Risk", back_populates="comments")
    user = relationship("User")


class Issue(Base):
    """问题模型"""
    name = Column(String(100), nullable=False, index=True)
    description = Column(Text, nullable=True)
    status = Column(String(20), nullable=False, default=IssueStatus.OPEN.value)
    priority = Column(String(20), nullable=False, default=IssuePriority.MEDIUM.value)
    reported_date = Column(DateTime, nullable=False)
    due_date = Column(DateTime, nullable=True)
    resolved_date = Column(DateTime, nullable=True)
    resolution = Column(Text, nullable=True)  # 解决方案
    
    # 外键
    project_id = Column(Integer, ForeignKey("project.id"), nullable=False)
    creator_id = Column(Integer, ForeignKey("user.id"), nullable=False)
    assignee_id = Column(Integer, ForeignKey("user.id"), nullable=True)
    related_risk_id = Column(Integer, ForeignKey("risk.id"), nullable=True)  # 关联风险
    
    # 关系
    project = relationship("Project", back_populates="issues")
    creator = relationship("User", foreign_keys=[creator_id], back_populates="created_issues")
    assignee = relationship("User", foreign_keys=[assignee_id], back_populates="assigned_issues")
    related_risk = relationship("Risk")
    comments = relationship("IssueComment", back_populates="issue")


class IssueComment(Base):
    """问题评论模型"""
    content = Column(Text, nullable=False)
    
    # 外键
    issue_id = Column(Integer, ForeignKey("issue.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("user.id"), nullable=False)
    
    # 关系
    issue = relationship("Issue", back_populates="comments")
    user = relationship("User")